package hash;

/**
 * 直接定址法设计复合类型的hash函数
 * @author yuisama
 * @date 2021/12/15 16:06
 **/
public class Student {
    public Student(int grade, int cls, String firstName, String lastName) {
        this.grade = grade;
        this.cls = cls;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    // 年级
    private int grade;
    // 班级
    private int cls;
    private String firstName;
    private String lastName;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return grade == student.grade && cls == student.cls && firstName.equals(student.firstName) && lastName.equals(student.lastName);
    }

    @Override
    public int hashCode() {
        // 直接定址法
        int B = 31;
        int hash = 0;
        hash = hash * B + Integer.hashCode(grade);
        hash = hash * B + Integer.hashCode(cls);
        hash = hash * B + firstName.hashCode();
        hash = hash * B + lastName.hashCode();
        return hash;
    }

    public static void main(String[] args) {
        Student stu1 = new Student(3,2,"yiming","liu");
        Student stu2 = new Student(3,2,"yiming","liu");
        System.out.println(stu1.hashCode());
        System.out.println(stu2.hashCode());
    }
}